import { Component, OnInit, Input, OnDestroy } from '@angular/core';
import { Router } from '@angular/router';
import { NzMessageService } from 'ng-zorro-antd';
import { TransferService } from '../service/transfer.service';
import { Subscription } from 'rxjs';

interface Title { translate?: string; name: string; }

@Component({
  selector: 'app-transfer-header',
  templateUrl: './transfer-header.component.html',
  styleUrls: ['./transfer-header.component.scss']
})
export class TransferHeaderComponent implements OnInit, OnDestroy {

  private _title: Title | string;

  // @Input()
  url: string;

  @Input()
  set title(_title: Title | string) {
    if (typeof _title === 'string') {
      this._title = {
        translate: _title,
        name: _title,
      };
    } else {
      this._title = _title;
    }
  }

  get title() {
    return this._title;
  }

  subscription: Subscription;

  toPage() {
    if (this.url) {
      this.router.navigateByUrl(this.url);
    } else {
      this.message.error('System error');
    }
  }

  constructor(
    private router: Router,
    private message: NzMessageService,
    private transferService: TransferService
  ) {
    this.url = '/member/center';
  }

  ngOnInit() {
     this.subscription = this.transferService.goBackUrl$.subscribe(url => {
      this.url = url;
    });

  }

  ngOnDestroy() {
    if (this.subscription) {
      this.subscription.unsubscribe();
    }
  }

}
